package net.pimpas.web.jsf.components.simplemenu;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;

import net.pimpas.resourcebundle.exception.MessageKeyNotFoundException;
import net.pimpas.resourcebundle.exception.ResourceBundleNotFoundException;
import net.pimpas.resourcebundle.serviceprovider.ResourceBundleServiceProvider;
import net.pimpas.resourcebundle.serviceprovider.ResourceBundleServiceProviderFactoryImpl;
import net.pimpas.resourcebundle.serviceprovider.ResourceBundleServiceProviderType;
import net.pimpas.web.jsf.components.UIComponentPimpasBase;
import net.pimpas.web.jsf.components.UIComponentPimpasBaseTag;
import net.pimpas.web.jsf.util.ComponentUtils;

public class UISimpleMenuTag extends UIComponentPimpasBaseTag {
	
	private String theme;
	private String value;
	private String onMenuItemClicked;
	private Integer width;
	private Integer height;
	private static final ResourceBundleServiceProvider bundle = 
		ResourceBundleServiceProviderFactoryImpl.getResourceBundleServiceProvider();
	
	@Override
	protected void setProperties(UIComponent component) {
		UISimpleMenu menu = (UISimpleMenu) component;
		if(this.getTheme()== null || "".equals(this.getTheme()))
			this.setTheme(UISimpleMenu.THEMES[UISimpleMenu.DEFAULT_THEME_INDEX]);
		
		if(this.getValue() == null || "".equals(this.getValue())) {
			try {
				throw new RuntimeException(bundle.getMessage(UIComponentPimpasBase.class, "propertyNull", ResourceBundleServiceProviderType.ERROR, "value", menu.getId()));
			} catch (ResourceBundleNotFoundException e) {
			} catch (MessageKeyNotFoundException e) {
			}
		}
		
		if(!ComponentUtils.isOnlyValueBinding(this.getValue())) {
			try {
				throw new RuntimeException(bundle.getMessage(UIComponentPimpasBase.class, "wrontValueForProperty", ResourceBundleServiceProviderType.ERROR, "value", menu.getId(), "valueBinding"));
			} catch (ResourceBundleNotFoundException e) {
			} catch (MessageKeyNotFoundException e) {
			}
		}
		
		menu.setTheme(this.getTheme());
		menu.setOnMenuItemClicked(this.getOnMenuItemClicked());
		menu.setValueBinding("value", FacesContext.getCurrentInstance().getApplication().createValueBinding(this.getValue()));
		
		if(this.getWidth() == null || this.getWidth() < 0)
			this.setWidth(UISimpleMenu.DEFAULT_WIDTH);
		if(this.getHeight() == null || this.getHeight() < 0)
			this.setHeight(UISimpleMenu.DEFAULT_HEIGHT);
		
		menu.setWidth(this.getWidth());
		menu.setHeight(this.getHeight());
		
		super.setProperties(component);
	}

	@Override
	public String getComponentType() {
		return UISimpleMenu.COMPONENT_TYPE;
	}

	public String getOnMenuItemClicked() {
		return onMenuItemClicked;
	}

	public void setOnMenuItemClicked(String onMenuItemClicked) {
		this.onMenuItemClicked = onMenuItemClicked;
	}

	public String getTheme() {
		return theme;
	}

	public void setTheme(String theme) {
		this.theme = theme;
	}

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}

	public Integer getHeight() {
		return height;
	}

	public void setHeight(Integer height) {
		this.height = height;
	}

	public Integer getWidth() {
		return width;
	}

	public void setWidth(Integer width) {
		this.width = width;
	}
}